import { useState } from 'react'
import { useRequest } from 'alova/client'
import { Button, Result, App } from 'antd'
import { Http } from '../../Utils/Api'
import { refreshStore } from '../../Config/zustand'
import { SetfieldsMapType } from '../../Config/theme'
import { useMount } from 'ahooks'
import { FieldMapping } from '../../Config/Mapping'
import { API_Mapping } from '../../Utils/Api'

const BatchIsEnabled = ({ data, Off }) => {
    const style = { width: 160, margin: 16 }
    const { options, init } = data
    const { batchActivationUrl, batchActivationBtnMap, batchActivationMapType, batchActivationParams } = options
    const { notification } = App.useApp()
    const { updateRefresh } = refreshStore()

    const [loading, setLoading] = useState(false)
    const [FieldsMapping, setFieldsMapping] = useState()

    const { send } = useRequest(params => Http({ url: batchActivationUrl, params }), { immediate: false })
    const onIsEnabled = (value) => {
        setLoading(true)
        const params = init.map(key => ({ uuid: key, [batchActivationParams]: value }))
        send(params).then(res => {
            notification.success({ key: 'fromModify', message: '操作成功', description: res.message })
            updateRefresh(new Date().getTime())
            setLoading(false)
            Off()
        })
    }

    useMount(async () => {
        if (batchActivationMapType === 'DataMapping') setFieldsMapping(FieldMapping[batchActivationBtnMap])
        if (batchActivationMapType === 'ApiMapping') {
            const data = await API_Mapping[batchActivationBtnMap]()
            setFieldsMapping(data)
        }
    })

    if (FieldsMapping) {
        return (
            <Result
                status="warning"
                title="您正在批量设置数据状态!"
                extra={
                    <div>
                        {FieldsMapping.map(({ label, value }) =>
                            <Button key={value} size='large' style={style} type="primary" loading={loading} onClick={() => onIsEnabled(value)} >{label}</Button>
                        )}
                    </div>
                }
            />
        )
    }
};

export default BatchIsEnabled;